import { ElMessage as $Message, ElLoading, ElMessageBox } from 'element-plus'
export const $msg = {
    loading: null,
    showLoading(...arg) {
        let { loading } = this;
        loading && loading.close();
        this.loading = ElLoading.service(...arg);
    },
    hideLoading(...arg) {
        let { loading } = this;
        loading && loading.close();
        this.loading = null;
    },
    showModal(...arg) {
        ElMessageBox(...arg);
    },
    message(...arg) {
        $Message(...arg);
    },
    success(...arg) {
        $Message.success(...arg);
    },
    warning(...arg) {
        $Message.warning(...arg);
    },
    error(...arg) {
        $Message.error(...arg);
    },
};

export const $loading = {
    t: null,
    num: 0,
    isLoading: true,
    show() {
        if (this.isLoading == false) return;
        if (0 == this.num) {
            clearTimeout(this.t);
            this.t = setTimeout(() => {
                $loading.hide();
                $msg.showLoading({
                    lock: true,
                    text: 'Loading',
                    background: 'rgba(0, 0, 0, 0.3)',
                })
            }, 500)
        }
        this.num++;
    },
    hide() {
        this.num--;
        if(0 >= this.num){
            clearTimeout(this.t);
            this.num = 0;
            $msg.hideLoading();
        }
    },
    clearAll(){
        clearTimeout(this.t);
        this.num = 0;
        $msg.hideLoading();
    }
};